* Table 4 Heterogenous impact - sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if bal_panel3==1

* identifiying marketing instruments, not provided in baseline year and to control farmers
local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	replace `var' = 0 if treat_year==0
	}

* creating baseline demographic vars
gen w1_age_head=.
gen w1_head_educ=.
forvalues a=1/18 {
replace w1_head_educ=a18_c`a' if a13_c`a' == 1 & mkt_year==2005
replace w1_age_head=a14_c`a' if a13_c`a' == 1 & mkt_year==2005
replace w1_head_educ=. if w1_age_head<18 & mkt_year==2005
replace w1_age_head=. if w1_age_head<18 & mkt_year==2005
replace w1_head_educ=0 if w1_head_educ==888 
replace w1_head_educ=0 if w1_head_educ==15 | w1_head_educ==16
}

replace sex=0 if sex==2
rename sex male
lab var male "=1 if male"

cap drop finlit_new
egen finlit_new=rowmean(l21c l22c l23c l32c l32c l33c l34c l35c l36c l41c l42c l43c l44c)


gen w1_hasplot = hasplot if mkt_year==2005
gen w1_finlit = finlit_new if mkt_year==2005
gen w1_wealth_index = wealth_index if mkt_year==2005

* creating above/below median variables
foreach var of varlist w1_finlit w1_head_educ w1_wealth_index {
replace `var'=. if `var'==888
sum `var' if mkt_year==2005 , d
gen `var'_high = (`var' >r(p50)) if mkt_year==2005
gen `var'_low = (`var' <=r(p50)) if mkt_year==2005
}

gen w1_hasplot_high=w1_hasplot if mkt_year==2005
gen w1_hasplot_low= (w1_hasplot==0) if mkt_year==2005

foreach var of varlist w1_finlit w1_head_educ w1_wealth_index w1_hasplot {
sort wave
bys id: egen `var'_high2=min(`var'_high)
bys id: egen `var'_low2=min(`var'_low)
drop `var'_high `var'_low
rename `var'_high2 `var'_high 
rename `var'_low2 `var'_low
}

*Define a sub-set of splitvars for regresion results;
local splitvar "w1_finlit_low w1_finlit_high w1_head_educ_low w1_head_educ_high w1_hasplot_high w1_hasplot_low  w1_wealth_index_low w1_wealth_index_high" 

* Creating table
local table = 1
local rastring "replace"
local outcomevars "totalrevenues_w1_real kval_w1_real fin_revenues_real totalcosts_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 fin_costs_real totalprofit_w1_real profit_k_w1_real  fraction_hy fraction_cashcrop plotsize_ha"


foreach split of varlist `splitvar' {
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2* firstt_2007 lost_w3 resurveyed if `split' == 1 , fe cluster(villageno) 
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../output/t4_`split'.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("`split'_`ovar'" ) ///
	`rastring' label stats(N) 

local rastring "append"

}
}

